﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EditPurchaseDetailsCtrl.ascx.cs" Inherits="WebUI.Controls.EditPurchaseDetailsCtrl" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script type="text/javascript">
    function roundNumber(num, dec) {
	    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }
    
    var sysGST = <%= SysVars.GST %>;
    function updateTotalAmt() {
        var unitPrice = document.getElementById("<%=txtUnitPrice.ClientID %>").value;
        if (unitPrice == '') unitPrice = 0;

        var qty = document.getElementById("<%=txtQuantity.ClientID %>").value;
        if (qty == '') qty = 1;
        if(qty == '0')
        {
            alert("Quantity = 0 is not allowed!");
            qty = 1;
            document.getElementById("<%=txtQuantity.ClientID %>").value = qty;
        }
        
        var gst = document.getElementById("<%=txtGST.ClientID %>").value;
        if (gst == '') gst = 1;
        

        var total = roundNumber(unitPrice * qty, 2);
        document.getElementById("<%=txtTotal.ClientID %>").value = total;
        
        if(gst > 0){
            gst = roundNumber(total * sysGST / (sysGST + 100), 2);
            document.getElementById("<%=txtGST.ClientID %>").value = gst;
        }
    }
    
    function onLostFocus(txt){        
        if(txt.value == ''){
            txt.value = '0';
        }
    }
    
    function onQtyLostFocus(txt){        
        var qty = txt.value;
        if (qty == '') qty = 1;
        if(qty == '0')
        {
            alert("Quantity = 0 is not allowed!");
            qty = 1;
            txt.value = qty;
        }
    }
</script>

<asp:Panel ID="mainPanel" runat="server">
    <asp:HiddenField ID="hidProductID" runat="server" />
    <table>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label1" runat="server" Text="Product:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ReadOnly="true" ID="txtProductName" Width="200px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label2" runat="server" Text="Unit Price:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ID="txtUnitPrice" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:CheckBox ID="chkUpdate" runat="server" Text="Update Product Cost When Saving" ToolTip="Save Change to System Product Cost." />
                <span class='labelNote'>(*)</span>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label3" runat="server" Text="Quantity:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ID="txtQuantity" Width="100px" runat="server" onchange="updateTotalAmt();" onblur="onQtyLostFocus(this);" ></asp:TextBox>
                <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtQuantity"  FilterType="Numbers"  runat="server">
                </cc1:FilteredTextBoxExtender>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label5" runat="server" Text="GST:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ReadOnly="true" ID="txtGST" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label6" runat="server" Text="Total Amount:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ReadOnly="true" ID="txtTotal" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
</asp:Panel>